using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;


namespace A1
{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    public class InputManager : Microsoft.Xna.Framework.GameComponent
    {

        GameTime gameTime;
        TimeSpan keyTime;
        TimeSpan previousKeyTime;
        TimeSpan previousScroll;
        TimeSpan allowScrolling;
        KeyboardState currentKeyboardState;
        KeyboardState previousKeyboardState;
        GamePadState currentGamepadState1;
        GamePadState previousGamepadState1;
        List<GamePadState> padStates;

        public InputManager(Game game)
            : base(game)
        {
            // TODO: Construct any child components here
        }

        /// <summary>
        /// Allows the game component to perform any initialization it needs to before starting
        /// to run.  This is where it can query for any required services and load content.
        /// </summary>
        public override void Initialize()
        {
            padStates = new List<GamePadState>();
            for (int i = 0; i < 8; i++)
            {
                padStates.Add(new GamePadState());
            }
            keyTime = TimeSpan.FromMilliseconds(10);
            previousKeyTime = TimeSpan.FromMilliseconds(0);
            previousScroll = TimeSpan.FromMilliseconds(0);
            allowScrolling = TimeSpan.FromMilliseconds(200);

            base.Initialize();
        }

        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)
        {

            for (int i = 0; i < 4; i++)
            {
                padStates[i + 4] = padStates[i];
                padStates[i] = GamePad.GetState((PlayerIndex)i);
            }
            previousKeyboardState = currentKeyboardState;
            currentKeyboardState = Keyboard.GetState();
            previousGamepadState1 = currentGamepadState1;
            currentGamepadState1 = GamePad.GetState(PlayerIndex.One);
            this.gameTime = gameTime;

            base.Update(gameTime);
        }
        /// <summary>
        /// Test whether we can allow a keypress
        /// </summary>
        /// <param name="testKey">The key to test</param>
        /// <returns></returns>
        public bool KeyReady(Keys testKey)
        {
            if (currentKeyboardState.IsKeyDown(testKey) && previousKeyboardState.IsKeyDown(testKey))
            {
                if (gameTime.TotalGameTime - previousScroll > allowScrolling)
                {
                    previousScroll = gameTime.TotalGameTime;
                    return true;
                }
            }
            return false;
        }
        /// <summary>
        /// Test whether we can allow a button press
        /// </summary>
        /// <param name="button">The button to check</param>
        /// <param name="playerIndex">The controller to check</param>
        /// <returns></returns>
        public bool ButtonReady(Buttons button, int playerIndex)
        {
            if (padStates[playerIndex].IsButtonDown(button) && padStates[playerIndex + 4].IsButtonDown(button))
            {
                if (gameTime.TotalGameTime - previousScroll > allowScrolling)
                {
                    previousScroll = gameTime.TotalGameTime;
                    return true;
                }
            }
            return false;

            /*
             
            if (currentGamepadState1.IsButtonDown(button) && previousGamepadState1.IsButtonDown(button))
            {
                if (gameTime.TotalGameTime - previousScroll > allowScrolling)
                {
                    previousScroll = gameTime.TotalGameTime;
                    return true;
                }
            }
            return false;
             */
        }

    }
}
